<?php

namespace Home\Controller;

use Lib;

class UserController extends Lib\Controller {
	
	function __construct() {
		//必须要调用父类
		parent::__construct ();
		
	}
	public function index() {
		$this->error ( ERROR_NO_METHODS, '参数错误，没有指明需要的操作' );
	}
	/**
	 * 用户登录
	 */
	public function login() {
		$d = $this->u->login ( $this->param['name'], $this->param['password'] );
		if (empty ( $d )) {
			$this->error ( ERROR_NO_USER, '没有查找到信息 | input : ' . $this->_input );
		}
		//计算key的值，保存到数据库
		$key = sha1(microtime(true) . $d['name']);
		$this->u->where('user_id = ' . $d['user_id'])->save(array('key'=>$key));
		$d['key'] = $key;
		$this->show ( $d );
	}
	/**
	 * 用户注册
	 */
	public function register(){
		$r = $this->u->register($this->param['name'],$this->param['password']);
		if(empty($r)){
			$this->error(ERROR_GENERIC,'失败' . $this->u->getError());
		}else{
			$this->show(array(id => $r));
		}
	}
	/**
	 * 检测用户名
	 */
	public function nameCheck(){
		$r = $this->nameCheck($this->param['name']);
		if(empty($r)){
			$this->show(array(user => true,msg => '用户已经存在'));
		}else{
			$this->show(array(user => false,msg => '用户不经存在'));
		}
	}
	
}